package com.cl.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import com.cl.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.annotation.SysLog;

import com.cl.entity.KaoshitongzhiEntity;
import com.cl.entity.view.KaoshitongzhiView;

import com.cl.service.KaoshitongzhiService;
import com.cl.service.TokenService;
import com.cl.utils.PageUtils;
import com.cl.utils.R;
import com.cl.utils.MPUtil;
import com.cl.utils.MapUtils;
import com.cl.utils.CommonUtil;

/**
 * 考试通知
 * 后端接口
 * @author 
 * @email 
 * @date 2025-02-18 12:38:56
 */
@RestController
@RequestMapping("/kaoshitongzhi")
public class KaoshitongzhiController {
    @Autowired
    private KaoshitongzhiService kaoshitongzhiService;







    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,KaoshitongzhiEntity kaoshitongzhi,
                                                                                                        HttpServletRequest request){
                    String tableName = request.getSession().getAttribute("tableName").toString();
                                                                                                    if(tableName.equals("xuesheng")) {
                    kaoshitongzhi.setXuehao((String)request.getSession().getAttribute("username"));
                                    }
                                                                                                            if(tableName.equals("jiaoshi")) {
                    kaoshitongzhi.setJiaoshigonghao((String)request.getSession().getAttribute("username"));
                                    }
                                                                                            EntityWrapper<KaoshitongzhiEntity> ew = new EntityWrapper<KaoshitongzhiEntity>();
                                                                                                                                                            
        
        
        PageUtils page = kaoshitongzhiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoshitongzhi), params), params));
        return R.ok().put("data", page);
    }
    
    
    
    
    
    
    
    /**
     * 前端列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,KaoshitongzhiEntity kaoshitongzhi, 
		HttpServletRequest request){
        EntityWrapper<KaoshitongzhiEntity> ew = new EntityWrapper<KaoshitongzhiEntity>();

		PageUtils page = kaoshitongzhiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoshitongzhi), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( KaoshitongzhiEntity kaoshitongzhi){
       	EntityWrapper<KaoshitongzhiEntity> ew = new EntityWrapper<KaoshitongzhiEntity>();
      	ew.allEq(MPUtil.allEQMapPre( kaoshitongzhi, "kaoshitongzhi")); 
        return R.ok().put("data", kaoshitongzhiService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(KaoshitongzhiEntity kaoshitongzhi){
        EntityWrapper< KaoshitongzhiEntity> ew = new EntityWrapper< KaoshitongzhiEntity>();
 		ew.allEq(MPUtil.allEQMapPre( kaoshitongzhi, "kaoshitongzhi")); 
		KaoshitongzhiView kaoshitongzhiView =  kaoshitongzhiService.selectView(ew);
		return R.ok("查询考试通知成功").put("data", kaoshitongzhiView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        KaoshitongzhiEntity kaoshitongzhi = kaoshitongzhiService.selectById(id);
		kaoshitongzhi = kaoshitongzhiService.selectView(new EntityWrapper<KaoshitongzhiEntity>().eq("id", id));
        return R.ok().put("data", kaoshitongzhi);
    }

    /**
     * 前端详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        KaoshitongzhiEntity kaoshitongzhi = kaoshitongzhiService.selectById(id);
		kaoshitongzhi = kaoshitongzhiService.selectView(new EntityWrapper<KaoshitongzhiEntity>().eq("id", id));
        return R.ok().put("data", kaoshitongzhi);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增考试通知")
    public R save(@RequestBody KaoshitongzhiEntity kaoshitongzhi, HttpServletRequest request){
        if(StringUtils.isNotEmpty(kaoshitongzhi.getXuehao())){
// 使用逗号分隔学号字符串，转换为数组或集合
            String[] xuehaoArray = kaoshitongzhi.getXuehao().split(",");
            List<String> xuehaoList = Arrays.asList(xuehaoArray);
            String[] xueshengxingmingArray = kaoshitongzhi.getSelectedXueshengxingming().split(",");
            List<String> xueshengxingmingList = Arrays.asList(xueshengxingmingArray);
            for (int i = 0; i < xuehaoList.size(); i++) {
                KaoshitongzhiEntity kaoshi = new KaoshitongzhiEntity();
                kaoshi.setXueshengxingming(xueshengxingmingList.get(i));
                kaoshi.setXuehao(xuehaoList.get(i));
                kaoshi.setAddtime(new Date());
                kaoshi.setJiaoshigonghao(kaoshitongzhi.getJiaoshigonghao());
                kaoshi.setJiaoshixingming(kaoshitongzhi.getJiaoshixingming());
                kaoshi.setTongzhishijian(kaoshitongzhi.getTongzhishijian());
                kaoshi.setTongzhineirong(kaoshitongzhi.getTongzhineirong());
                kaoshitongzhiService.insert(kaoshi);
            }
        }
    	//ValidatorUtils.validateEntity(kaoshitongzhi);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @SysLog("新增考试通知")
    @RequestMapping("/add")
    public R add(@RequestBody KaoshitongzhiEntity kaoshitongzhi, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(kaoshitongzhi);
        kaoshitongzhiService.insert(kaoshitongzhi);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改考试通知")
    public R update(@RequestBody KaoshitongzhiEntity kaoshitongzhi, HttpServletRequest request){
        //ValidatorUtils.validateEntity(kaoshitongzhi);
        kaoshitongzhiService.updateById(kaoshitongzhi);//全部更新
        return R.ok();
    }



    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除考试通知")
    public R delete(@RequestBody Long[] ids){
        kaoshitongzhiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
	









}
